import {
  financeMessagNumAPI
} from '@/api/financemanagement/financemessage'

/**
 * 消息记录
 */
const app = {
  state: {
    // 待办事项消息
    financemessageNum: {
      devPurchase: 0,
      normalPurchase: 0,
      officePurchase: 0,
      otherPurchase: 0,
      projectPurchase: 0,
      totalNum: 0
    }
  },

  mutations: {
    /**
     * 更改待办事项
     */
    SET_FINANCEMESSAGENUM: (state, financemessageNum) => {
      let totalNum = 0
      for (const key in financemessageNum) {
        if (key != 'totalNum') {
          totalNum += (financemessageNum[key] || 0)
        }
      }
      financemessageNum.totalNum = totalNum
      state.financemessageNum = financemessageNum
    }
  },

  actions: {
    // 登录
    GetFinanceMessageNum({
      state,
      commit
    }) {
      return new Promise((resolve, reject) => {
        financeMessagNumAPI()
          .then(response => {
            commit('SET_FINANCEMESSAGENUM', response.data)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    }

  }
}

export default app
